#!/usr/bin/env python
import os
import sys

#Change SConscript path to godot-cpp SConstruct
env = SConscript("../godot-cpp-4.2/SConstruct")

# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags
gdxEnv = env.Clone()
# tweak this if you want to use different folders, or more folders, to store your source code in.

gdxEnv.AppendUnique( CPPPATH=["src/", "includes/", "libinc/"] )
gdxEnv.AppendUnique( LIBPATH=["lib/"] )

#Change lib base to MSYS2 subsystem libs.
if gdxEnv["platform"] == "android":
    gdxEnv.AppendUnique( LINKFLAGS=["--no-undefined"])
    gdxEnv['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME']=1
else:
    if gdxEnv["CPPDEFINES"].count("DEBUG_ENABLED") > 0:
        gdxEnv.AppendUnique( CXXFLAGS=["-fexceptions"])
    gdxEnv.AppendUnique( 
        LIBS=[ 'memoryMgr', 'pathbuildercore', 'protobuf' ]
    )

sources = [
    Glob("src/*.cpp")
    ]
objs = []
for dirs in sources:
    for p in dirs:
        tPath = "obj/{}.{}".format(p.name, "o")
        gdxEnv.Object(target=tPath, source=p)
        objs.append(tPath)

if gdxEnv["platform"] == "macos":
    library = gdxEnv.SharedLibrary(
        "bin/path_builder.{}.{}.framework/path_builder.{}.{}".format(
            gdxEnv["platform"], gdxEnv["target"], gdxEnv["platform"], gdxEnv["target"]
        ),
        source=objs,
    )
else:
    library = gdxEnv.SharedLibrary(
        "bin/path_builder{}{}".format(gdxEnv["suffix"], gdxEnv["SHLIBSUFFIX"]),
        source=objs,
    )

Default(library)
